package cn.iocoder.yudao.module.trade.controller.admin.order.vo;

import cn.iocoder.yudao.module.trade.enums.order.TradeOrderAuditStatusEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderDeliveryStatusEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderStatusEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import java.math.BigDecimal;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 交易订单 Base VO，提供给添加、修改、详细的子 VO 使用
 * 如果子 VO 存在差异的字段，请不要添加到这里，影响 Swagger 文档生成
 */
@Data
public class TradeOrderBaseVO {

    // ========== 订单基本信息 ==========

    @Schema(description = "订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    private Long id;

    @Schema(description = "订单流水号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1146347329394184195")
    private String no;

    @Schema(description = "下单时间", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime createTime;

    @Schema(description = "订单类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer type;

    @Schema(description = "订单来源", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer terminal;

    @Schema(description = "用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
    private Long userId;

    @Schema(description = "用户 IP", requiredMode = Schema.RequiredMode.REQUIRED, example = "127.0.0.1")
    private String userIp;

    @Schema(description = "用户备注", requiredMode = Schema.RequiredMode.REQUIRED, example = "你猜")
    private String userRemark;

    @Schema(description = "订单状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer status;

    @Schema(description = "购买的商品数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
    private Integer productCount;

    @Schema(description = "订单完成时间")
    private LocalDateTime finishTime;

    @Schema(description = "订单取消时间")
    private LocalDateTime cancelTime;

    @Schema(description = "取消类型", example = "10")
    private Integer cancelType;

    @Schema(description = "商家备注", example = "你猜一下")
    private String remark;

    // ========== 价格 + 支付基本信息 ==========

    @Schema(description = "支付订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    private Long payOrderId;

    @Schema(description = "是否已支付", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
    private Boolean payStatus;

    @Schema(description = "付款时间")
    private LocalDateTime payTime;

    @Schema(description = "支付渠道", requiredMode = Schema.RequiredMode.REQUIRED, example = "wx_lite")
    private String payChannelCode;

    @Schema(description = "商品原价（总）", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000")
    private Integer totalPrice;

    @Schema(description = "订单优惠（总）", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer discountPrice;

    @Schema(description = "运费金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer deliveryPrice;

    @Schema(description = "订单调价（总）", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer adjustPrice;

    @Schema(description = "应付金额（总）", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000")
    private Integer payPrice;

    // ========== 收件 + 物流基本信息 ==========

    @Schema(description = "配送方式", example = "10")
    private Integer deliveryType;

    @Schema(description = "自提门店", example = "10")
    private Long pickUpStoreId;

    @Schema(description = "自提核销码", example = "10")
    private Long pickUpVerifyCode;

    @Schema(description = "配送模板编号", example = "1024")
    private Long deliveryTemplateId;

    @Schema(description = "发货物流公司编号", example = "1024")
    private Long logisticsId;

    @Schema(description = "发货物流单号", example = "1024")
    private String logisticsNo;

    @Schema(description = "发货时间")
    private LocalDateTime deliveryTime;

    @Schema(description = "收货时间")
    private LocalDateTime receiveTime;

    @Schema(description = "收件人名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "张三")
    private String receiverName;

    @Schema(description = "收件人手机", requiredMode = Schema.RequiredMode.REQUIRED, example = "13800138000")
    private String receiverMobile;

    @Schema(description = "收件人地区编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "110000")
    private Integer receiverAreaId;

    @Schema(description = "收件人详细地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "中关村大街 1 号")
    private String receiverDetailAddress;

    // ========== 售后基本信息 ==========

    @Schema(description = "售后状态", example = "1")
    private Integer afterSaleStatus;

    @Schema(description = "退款金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer refundPrice;

    // ========== 营销基本信息 ==========

    @Schema(description = "优惠劵编号", example = "1024")
    private Long couponId;

    @Schema(description = "优惠劵减免金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer couponPrice;

    @Schema(description = "积分抵扣的金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer pointPrice;

    @Schema(description = "VIP 减免金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "888")
    private Integer vipPrice;

    @Schema(description = "推广人编号", example = "1")
    private Long brokerageUserId;

    @Schema(description = "发货状态[0/待发货 1/已发货 2/发货失败]", example = "1")
    private Integer deliveryStatus;

    @Schema(description = "发货失败原因", example = "库存不足")
    private String deliveryReason;

    @Schema(description = "订单币种", example = "CNY")
    private String orderCurrency;

    @Schema(description = "汇率", example = "7.2")
    private BigDecimal exchangeRate;

    @Schema(description = "调用支付时价格（总），单位：分", example = "10000")
    private Integer paymentPrice;

    @Schema(description = "调用支付时的币种", example = "CNY")
    private String paymentCurrency;

    @Schema(description = "下单地区编码", example = "CHINA")
    private String orderAreaCode;

    @Schema(description = "下单地区名称", example = "中国")
    private String orderAreaName;

    @Schema(description = "审核状态[0/待审核 1/审核通过 2/审核不通过 3/已取消]", example = "1")
    private Integer auditStatus;

    @Schema(description = "审核原因", example = "测试订单审核不通过")
    private String auditReason;

    @Schema(description = "需人工处理", example = "1")
    private Integer manual = 0;

    // ========== 活动基本信息 ==========
    /**
     * 拼团活动编号
     *
     * 关联 CombinationActivityDO 的 id 字段
     */
    @Schema(description = "拼团活动编号", example = "1")
    private Long combinationActivityId;
    /**
     * 拼团团长编号
     *
     * 关联 CombinationRecordDO 的 headId 字段
     */
    @Schema(description = "拼团团长编号", example = "1")
    private Long combinationHeadId;
    /**
     * 拼团记录编号
     *
     * 关联 CombinationRecordDO 的 id 字段
     */
    @Schema(description = "拼团记录编号", example = "1")
    private Long combinationRecordId;

    /**
     * 分享状态[0/无需分享 1/未分享 2/已分享]
     */
    @Schema(description = "分享状态", example = "1")
    private Integer shareStatus;

    @Schema(description = "商品总额, 折算汇率后, 单位：分", example = "10000")
    private Integer totalPaymentPrice;

    /**
     * 激活状态
     */
    @Schema(description = "激活状态", example = "1")
    private Integer activeState;

    @Schema(description = "渠道订单号")
    private String channelOrderNo;

    /**
     * 发卡渠道订单id
     */
    private String cdkChannelOrderId;

    /**
     * 发卡渠道内部订单id
     */
    @Schema(description = "发卡渠道内部订单id", example = "bamboo订单号")
    private String cdkChannelInnerOrderId;

    /**
     * 商品名称
     */
    @Schema(description = "商品名称", example = "商品名称")
    private String skuName;

    public boolean checkManual() {
        return TradeOrderStatusEnum.isUndelivered(this.getStatus())
            && (
            TradeOrderAuditStatusEnum.isUnAudited(this.getAuditStatus())
                || TradeOrderDeliveryStatusEnum.deliverFailed(this.getDeliveryStatus())
        );
    }

}
